﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

/// <summary>
/// 缓存
/// </summary>
public class WebCache
{
    protected Cache curCache;
    public WebCache()
    {
        curCache = HttpContext.Current.Cache;
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="strKey"></param>
    /// <param name="objValue"></param>
    public void Insert(string strKey, object objValue)
    {
        if (!Has(strKey))
        {
            CacheItemRemovedCallback callbackFun = new CacheItemRemovedCallback(EndCacheLog);
            curCache.Insert(
                strKey, 
                objValue, 
                null, 
                Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 7:30")), 
                Cache.NoSlidingExpiration, 
                CacheItemPriority.Default, 
                callbackFun);
        }
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <param name="strKey">键值</param>
    /// <returns>数据</returns>,
    public object Get(string strKey)
    {
        return curCache[strKey];
    }

    /// <summary>
    /// 判断键值是否存在
    /// </summary>
    /// <param name="key">键值</param>
    /// <returns>bool</returns>
    public bool Has(string strKey)
    {
        return curCache[strKey] != null;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="strKey"></param>
    /// <param name="objValue"></param>
    public void Update(string strKey, object objValue)
    {
        curCache[strKey] = objValue;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="strKey">键值</param>
    /// <returns>bool</returns>
    public bool Remove(string strKey)
    {
        return curCache.Remove(strKey) != null;
    }

    /// <summary>
    /// 日志
    /// </summary>
    public void EndCacheLog(string key, object value, CacheItemRemovedReason res)
    {
        Common.Log("清除缓存" + key);
    }
}